#include "wxsimagelist.h"
#include "../properties/wxsimagelisteditordlg.h"

namespace {
  wxsRegisterItem<wxsImageList> Reg( _T( "ImageList" ), _T( "ImageList" ), wxsTTool, _T( "工具" ), 72 );
}

wxsImageList::wxsImageList( wxsItemResData *Data ):
  wxsTool( Data, &Reg.Info, 0, 0 ) {
  int n;
  wxString ss, tt;
  wxFileName fn;
  m_IsBuilt = false;
  m_ImageData.Clear();
  m_Width = 16;
  m_Height = 16;
  m_Count = 0;
  m_Include = false;
  fn = Data->GetSrcFileName();
  ss = fn.GetPath( ( wxPATH_GET_VOLUME + wxPATH_GET_SEPARATOR ) );
  n  = ss.Len();
  ss = ss + _T( "wximages" ) + ss[n - 1];
  m_IDir = ss;
  m_RDir = _T( "./wximages/" );
  m_Base = fn.GetName();
}

void wxsImageList::OnBuildCreatingCode() {
  int         i;
  wxString    inc;
  wxString    vname;
  wxString    bname;
  wxString    fbase;
  wxString    fabs;
  wxString    frel;
  wxString    dname;
  wxBitmap    bmp;
  wxString    ss, tt;
  if( m_IsBuilt ) {
    return;
  }
  m_IsBuilt = true;
  switch( GetLanguage() ) {
    case wxsCPP: {
      AddHeader( _T( "<wx/imaglist.h>" ), GetInfo().ClassName, 0 );
      StoreXpmData();
      vname = GetVarName();
      if( m_Count == 0 ) {
        Codef( _T( "%s = new wxImageList(%d, %d, 1);\n" ), vname.wx_str(), m_Width, m_Height );
      } else {
        Codef( _T( "%s = new wxImageList(%d, %d, %d);\n" ),  vname.wx_str(), m_Width, m_Height, ( m_Count + 1 ) );
        for( i = 0; i < m_Count; i++ ) {
          ss.Printf( _T( "%s_%d_XPM" ), vname.wx_str(), i );
          Codef( _T( "%s->Add(wxBitmap(%s));\n" ), vname.wx_str(), ss.wx_str() );
        }
      }
      BuildSetupWindowCode();
      return;
    }
    case wxsUnknownLanguage:
    default: {
      wxsCodeMarks::Unknown( _T( "wxsImageList::OnBuildCreatingCode" ), GetLanguage() );
    }
  }
}


void wxsImageList::OnEnumToolProperties( M_Code_unused long Flags ) {
  m_IsBuilt = false;
  m_Context = GetCoderContext();
  WXS_IMAGELIST( wxsImageList, m_ImageData, _T( "Image List" ), _T( "image_list" ) );
  WXS_ARRAYSTRING( wxsImageList, m_ImageData, _T( "Images as Text" ), _T( "image_text" ), _T( "item2" ) );
  WXS_LONG( wxsImageList, m_Width,   _T( "Image Width" ),      _T( "image_width" ),   16 );
  WXS_LONG( wxsImageList, m_Height,  _T( "Image Height" ),     _T( "image_height" ),  16 );
  WXS_LONG( wxsImageList, m_Count,   _T( "Image Count" ),      _T( "image_count" ),   0 );
  WXS_BOOL( wxsImageList, m_Include, _T( "Use Include File" ), _T( "use_include" ), false );
};


void wxsImageList::StoreXpmData( void ) {
  int         i, n;
  long        ll;
  wxString    vname;
  wxString    xname;
  wxString    ss, tt, vv;
  wxFile      ff;
  vname = GetVarName();
  n = m_ImageData.GetCount();
  if( n <= 2 ) {
    if( m_Width <= 0 ) {
      m_Width = 16;
    }
    if( m_Height <= 0 ) {
      m_Height = 16;
    }
    m_Count = 0;
    m_ImageData.Clear();
    ss.Printf( _T( "%ld" ), m_Width );
    m_ImageData.Add( ss );
    ss.Printf( _T( "%ld" ), m_Height );
    m_ImageData.Add( ss );
  } else {
    ss = m_ImageData.Item( 0 );
    if( ss.ToLong( &ll ) ) {
      m_Width = ll;
    }
    ss = m_ImageData.Item( 1 );
    if( ss.ToLong( &ll ) ) {
      m_Height = ll;
    }
    m_Count = 0;
    for( i = 0; i < ( int )m_ImageData.GetCount(); i++ ) {
      ss = m_ImageData.Item( i );
      if( ss.Find( _T( "xpm_data" ) ) >= 0 ) {
        m_Count += 1;
      }
    };
  };
  if( m_Count == 0 ) {
    return;
  }
  if( m_Include ) {
    if( ! wxFileName::DirExists( m_IDir ) ) {
      wxFileName::Mkdir( m_IDir );
    }
    ss  = m_IDir;
    ss += m_Base;
    ss += _T( "_" );
    ss += vname;
    ss += _T( ".xpm" );
    ff.Open( ss, wxFile::write );
  }
  n = 0;
  i = 2;
  tt = _T( "" );
  while( i < ( int )m_ImageData.GetCount() ) {
    ss = m_ImageData.Item( i );
    i += 1;
    if( ss.Find( _T( "xpm_data" ) ) >= 0 ) {
      vv.Printf( _T( "%s_%d_XPM" ), vname.wx_str(), n );
      ss.Replace( _T( "xpm_data" ), vv );
      n += 1;
      if( tt.Length() > 0 ) {
        if( m_Include ) {
          ff.Write( tt );
        } else
        { Codef( tt ); }
      }
      tt  = ss;
      tt += _T( "\n" );
    } else {
      tt += ss;
      tt += _T( "\n" );
    }
  }
  if( tt.Length() > 0 ) {
    if( m_Include ) {
      ff.Write( tt );
    } else
    { Codef( tt ); }
  }
  if( m_Include ) {
    ff.Close();
    ss = _T( "\"" );
    ss += m_RDir;
    ss += m_Base;
    ss += _T( "_" );
    ss += vname;
    ss += _T( ".xpm" );
    ss += _T( "\"" );
    AddHeader( ss, GetInfo().ClassName, 0 );
  }
}

int  wxsImageList::GetCount( void ) {
  return m_Count;
}


wxBitmap wxsImageList::GetPreview( int inIndex ) {
  int             i, j, n;
  wxString        ss, tt;
  wxArrayString   aa;
  wxBitmap        bmp;
  if( ( inIndex < 0 ) || ( inIndex >= m_Count ) ) {
    return wxNullBitmap;
  }
  if( m_ImageData.GetCount() == 0 ) {
    return wxNullBitmap;
  }
  n = -1;
  j = 0;
  i = 0;
  while( ( i < ( int )m_ImageData.GetCount() ) && ( n < 0 ) ) {
    ss = m_ImageData.Item( i );
    i += 1;
    if( ss.Find( _T( "xpm_data" ) ) >= 0 ) {
      if( j == inIndex ) {
        n = i;
      }
      j += 1;
    }
  }
  if( n < 0 ) {
    return wxNullBitmap;
    \
  }
  aa.Clear();
  aa.Add( ss );
  i = n;
  n = -1;
  while( ( i < ( int )m_ImageData.GetCount() ) && ( n < 0 ) ) {
    ss = m_ImageData.Item( i );
    i += 1;
    if( ss.Find( _T( "xpm_data" ) ) >= 0 ) {
      n = i;
    } else
    { aa.Add( ss ); }
  }
  wxsImageListEditorDlg::ArrayToBitmap( aa, bmp );
  return bmp;
}


void wxsImageList::GetImageList( wxImageList &aImageList ) {
  aImageList.RemoveAll();
  wxsImageListEditorDlg::ArrayToImageList( m_ImageData, aImageList );
}


void wxsImageList::DoBuild( void ) {
  BuildCode( m_Context );
}

